package com.jxpowernode.bitcon.btcrpccode;

import com.alibaba.fastjson.JSONObject;
import com.jxpowernode.bitcon.Constants;
import com.jxpowernode.bitcon.bean.rpcbtc.GetBestBlock;
import com.jxpowernode.bitcon.request.HttpUtils;
import com.jxpowernode.bitcon.service.btcservice.GetBitconService;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Lixuewei
 * @Email:1410511702@qq.com
 * @Desc: 该类用户实现通过rpc访问调用btc的getbestblockhash方法
 * @Date: 2021/12/13 17:08
 */
public class GetBestBlockHash {

    /**
     * 该方法用于获取区块hash值
     * @param id 区块id
     * @return 返回区块hash值
     */
    public GetBestBlock getBestBlockHash(int id ){
        GetBitconService getBitconService = new GetBitconService();
        Map<String, String> header = getBitconService.getBitcon();
        Map<String,Object> boby = new HashMap<>();
        boby.put("id",id);
        boby.put("method","getbestblockhash");
        boby.put("params",null);
        boby.put("json_rpc","2.0");
        byte[] data = JSONObject.toJSONBytes(boby);
        String result = HttpUtils.post(Constants.RPC_URL, header, data);
        GetBestBlock parse =  JSONObject.parseObject(result, GetBestBlock.class);
        return parse;
    }
}
